package com.junlinpro.udp;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UdpServer {

    public void run(int port) {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.
                    group(eventLoopGroup).
                    channel(NioDatagramChannel.class).
                    option(ChannelOption.SO_BROADCAST, true).
                    handler(new UdpServerHandler());
            bootstrap.bind(port).sync().channel().closeFuture().await();
            ChannelFuture f = bootstrap.bind(port).sync();
            if (f.isSuccess()) {            	
            	log.info("----- UDP 服务加载成功 -----");
            }
        } catch (InterruptedException e) {
            log.error(e.getMessage());
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
}
